﻿using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.Collections;
public class UIManager : SparkSingleton<UIManager>
{
    private UIRoot uiRoot;
    public string prefabPath = "Client/UI/";
    private List<MenuBase> menus = new List<MenuBase>();
    private MenuBase currentMenu;

    public void setPrefabPath(string path)
    {
        prefabPath = path;
    }

    private Transform UIRootTrans
    {
        get
        {
            if (uiRoot == null)
            {
                uiRoot = FindObjectOfType<UIRoot>();
            }
            return uiRoot.transform;
        }
    }
    void Awake()
    {
        if (instance == null)
        {
            Debug.Log("UImanager DontDestroyOnLoad this = " + this.GetInstanceID());
            DontDestroyOnLoad(this);
        }
        else
        {
            Debug.Log("destroy this  ui,this = "+this.GetInstanceID());
            Destroy(this.cachedGameObject);
        }
    }
    public T popMenu<T>(string name) where T : MenuBase
    {
        GameObject menu = popMenu(name);
        T menuBase = menu.GetComponent<T>();
        return menuBase;
    }

    public GameObject popMenu(string name)
    {
        GameObject menu = Resources.Load<GameObject>(prefabPath + name).Clone();
        //menu.GetComponent<UIPanel>().depth = menus.Count + 1;
        menu.transform.parent = UIRootTrans;
        menu.transform.localScale = Vector3.one;
        menu.transform.localPosition = Vector3.zero;
        menu.name = name;
        MenuBase mBase = menu.GetComponent<MenuBase>();
        menus.Push(mBase);
        currentMenu = mBase;
        return menu;
    }

    public MenuBase CurrentMenu
    {
        get { return currentMenu; }
    }

    public void unpopMenu(string name)
    {
        GameObject menu = uiRoot.gameObject.FindChild(name);
        if (menu != null)
        {
            unpopMenu(menu.GetComponent<MenuBase>());
        }
    }

    public void unpopMenu()
    {
        unpopMenu(currentMenu);
    }

    public void unpopMenu(MenuBase menu)
    {
        menu.unpopMe();
    }

    public void removeAndDestroy(MenuBase menu)
    {
        if (menus.Contains(menu))
        {
            menus.Remove(menu);
        }
        if (currentMenu == menu)
        {
            if (menus.Count > 0)
            {
                currentMenu = menus[menus.Count - 1];
            }
            else
            {
                currentMenu = null;
            }

        }
        menu.DestroyObject();
    }

    public void unpopMenu<T>() where T : MenuBase
    {
        MenuBase menu = menus.SingleOrDefault(m => m is T);
        if (menu != null)
        {
            unpopMenu(menu);
        }
    }

    public T getMenu<T>() where T : MenuBase
    {
        return menus.OfType<T>().SingleOrDefault();
    }
//     public IEnumerator DelayLoadMainMenu(string menu,float time)
//     {
//         yield return new WaitForSeconds(time);
//         this.LoadMainMenuUI(menu);
//     }
}

